package com.tevintao.marketingsystem.task;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.TaskLogicService;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.HookWindowManagerGlobal;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import java.util.List;


/**
 * 上报通讯录
 */
public class UploadContactsProcess extends SetTaskExtra {
    private static UploadContactsProcess ourInstance = new UploadContactsProcess();

    public static UploadContactsProcess getInstance() {
        return ourInstance;
    }

    private UploadContactsProcess() {
    }


    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            View menuView = v.findViewById(2);
            if (null != menuView) {
                ViewOperation.clickView(menuView);
            }

            View addFriend = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, 2131624112, "添加朋友");
            if (null != addFriend) {
                ViewOperation.clickView(addFriend);
            }
        } else if (WxUiViewConfig.VIEW_SELECT_ADD_FRIEND_TYPE.equals(contextName)) {
//            WxUiViewConfig.printViewGroup((ViewGroup) v);
            View subscribeView = WxUiViewConfig.getViewByIdAndContainContent((ViewGroup) v, 16908310, "手机联系人");
            if (null != subscribeView) {
                ViewOperation.clickView(subscribeView);
            }
        } else if (WxUiViewConfig.VIEW_SELECT_ADD_FRIEND_TYPE_2.equals(contextName)) {
//            WxUiViewConfig.printViewGroup((ViewGroup) v);
            View phoneContacts = WxUiViewConfig.getViewByIdAndContainContent((ViewGroup) v, WxUiViewConfig.KEY_TYPE_PHONE_CONTACTS, "添加手机联系人");
            if (null != phoneContacts) {
                ViewOperation.clickView(phoneContacts);
            }

        } else if (WxUiViewConfig.VIEW_LOOK_PHONE_CONTACTS_LIST.equals(contextName)) {
            View noContactsView = v.findViewById(WxUiViewConfig.KEY_NO_CONTACTS);
            if (null != noContactsView && noContactsView.getVisibility() == View.VISIBLE) {
                taskError(v.getContext(), WxTaskManger.TASK_SATE_FIND_FRIEND_ERROR, "无联系人");
                return true;
            }

            View noResult = v.findViewById(WxUiViewConfig.KEY_NO_RESULT);
            if (null != noResult && noResult.getVisibility() == View.VISIBLE) {
                taskError(v.getContext(), WxTaskManger.TASK_SATE_FIND_FRIEND_ERROR, "无联系人");
                return true;
            }
            boolean loading = false;
            List<View> views = HookWindowManagerGlobal.getInstance().getViews();
            for (View vv : views) {
                Context context = vv.getContext();
                LogManager.getInstance().logInfo("context = " + context);
                String dialogName = context.getClass().getName();
                if (WxUiViewConfig.DIALOG_CLASS.equals(dialogName)) {
                    WxUiViewConfig.printViewGroup((ViewGroup) vv);
                    loading = true;
                }
            }
            if (loading) {
                LogManager.getInstance().logError("正在查找朋友信息");
                return true;
            } else {
//                ArrayList<View> findViews = new ArrayList<>();
//                View addView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, 2131625768, "添加");
//                if (null != addView && addView.getVisibility() == View.VISIBLE) {
//                    ViewOperation.clickView(addView);
//                    taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "执行完毕");
//                    return true;
//                }
                taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "执行完毕");
            }
        } else if (WxUiViewConfig.VIEW_CONTACT_INFO_UI.equals(contextName)) {
            inputKeyEnter(v.getContext());
        } else if(WxUiViewConfig.VIEW_UPDATE_CONTACTS.equals(contextName)) {
            LogManager.getInstance().logError("VIEW_UPDATE_CONTACTS = " + WxUiViewConfig.VIEW_UPDATE_CONTACTS);
            WxUiViewConfig.printViewGroup((ViewGroup) v);
            View updateContacts = v.findViewById(WxUiViewConfig.VIEW_REPORT_CONTACTS);
            if (null != updateContacts) {
                ViewOperation.clickView(updateContacts);
            }
        } else if (WxUiViewConfig.DIALOG_CLASS.equals(contextName)) {
            View ok = v.findViewById(WxUiViewConfig.DIALOG_INFO_OK);
            if (null != ok) {
                ViewOperation.clickView(ok);
            }
        }
        return false;
    }

    private void inputKeyEnter(Context context) {
        Intent intent = new Intent(TaskLogicService.ACTION_KEY_RECEIVER);
        Bundle bundle = new Bundle();
        bundle.putInt(TaskLogicService.KEY_INPUT_KEY, TaskLogicService.INPUT_TYPE_BACK);
        intent.putExtras(bundle);
        context.sendBroadcast(intent);
    }
}
